package com.cs.peanut.player;

import android.Manifest;
import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cs.peanut.base.util.TLog;
import com.cs.peanut.base.util.adapter.BaseQuickAdapter;
import com.cs.peanut.base.util.adapter.OnItemChildClickListener;
import com.cs.peanut.base.util.adapter.QuickAdapter;
import com.cs.peanut.base.util.adapter.VHolder;
import com.cs.peanut.base.util.file.FileManager;
import com.cs.peanut.base.util.file.Video;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class MainActivity extends AppCompatActivity {
    RxPermissions mRxPermissions;
    FileManager fileManager;
    List<Bitmap> videoBitmapList;
    QuickAdapter<Bitmap> mAdapter;
    List<Video> videoList;
    AlertDialog mDialog;
    @BindView(R.id.recycleView)
    RecyclerView mRecyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mAdapter = new QuickAdapter<Bitmap>(R.layout.item_video,videoBitmapList) {
            @Override
            protected void convert(VHolder holder, Bitmap bitmap) {
                holder.addOnClickListener(R.id.video);
                holder.setImageBitmap(R.id.video,bitmap);
            }
        };
        fileManager = FileManager.getInstance(this);
        mRxPermissions = new RxPermissions(MainActivity.this);
        mRxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(granted -> {
                    if (granted) {
                        videoList = FileManager.getVideos();
                        videoBitmapList = new ArrayList<>();
                        for (Video video : videoList) {
                            videoBitmapList.add(FileManager.getVideoThumbnail(video.getId()));
                        }
                        if(!videoBitmapList.isEmpty()){
                            mAdapter.setNewData(videoBitmapList);
                        }
                    }
                });
        mRecyclerView.setLayoutManager(new GridLayoutManager(this,4));
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Video video = videoList.get(position);
                mDialog = new TransferDialog.TransferBuild(MainActivity.this, video.getPath(), "192.168.1.115",mOnFisish).create();
                mDialog.show();
            }
        });
    }

    TransferDialog.OnFisish mOnFisish = new TransferDialog.OnFisish() {
        @Override
        public void finish() {
            mDialog.dismiss();
        }
    };
}
